package leetcodecompetition.S100;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

public class T6351 {
    public long findScore(int[] nums) {
        long sorce=0;
        int len=nums.length;
        int [][]ints=new int[nums.length][2];
        Set<Integer> hash=new HashSet<>();
        for (int i = 0; i < len; i++) {
            ints[i][1]=i;
            ints[i][0]=nums[i];
        }
        Arrays.sort(ints, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                if (o1[0]==o2[0])return o1[1]-o2[1];
                return o1[0]-o2[0];
            }
        });

        for (int i = 0; i <len ; i++) {
            int index=ints[i][1];
            if (hash.add(index)){
//                System.out.println(ints[i][0]);
                sorce+=ints[i][0];
                if (index-1>=0)hash.add(index-1);
                if (index+1<len)hash.add(index+1);
            }
        }
        return sorce;
    }

    @Test
    public void test() {
        int []ints={2,1,3,4,5,2};
        System.out.println(findScore(ints));
    }
}
